#include "TCPServer.h"
#include <QThread>
#include <QDebug>
#include <QtNetwork>

#include <QUdpSocket>

#include "TcpConn.h"

TCPServer::TCPServer(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::TCPServerClass())

	, m_tcpServer(nullptr)
{
    ui->setupUi(this);
    qDebug() << "TCPServer main thread" << QThread::currentThread() << QThread::currentThreadId() << this->thread();

    //use sub thread
	//TcpConn* conn = new TcpConn(this);
	//conn->listen(QHostAddress("10.1.120.40"), 1234);

    //use main thread
    m_tcpServer = new QTcpServer(this);
	m_tcpServer->listen(QHostAddress("192.168.126.56"), 5003);
	connect(m_tcpServer, &QTcpServer::newConnection, this, &TCPServer::conn);

	//use UDP
	m_udpSocket = new QUdpSocket();
	//m_udpSocket->bind(QHostAddress(QString("10.1.125.221")), 5001);
	connect(m_udpSocket, &QUdpSocket::readyRead, [&]() 
		{
			QByteArray datagram;
			if (m_udpSocket->hasPendingDatagrams())
			{
				datagram.resize(m_udpSocket->pendingDatagramSize());
				QHostAddress addr;
				quint16 port = 0;
				m_udpSocket->readDatagram(datagram.data(), datagram.size(), &addr, &port);
				if (datagram.size() != 0)
				{
					//qDebug() << "recv data" << datagram.toHex(' ');
				}

				{
					QByteArray data;
					static int step = 0;
					if (step == 0)//OK
					{
						step = 0;
						qDebug() << datagram.toHex(' ');


						data = QByteArray::fromHex("55 A1 04 02 00 00 00 00 00 00 00 00 06 ff ee");
						if (!data.isEmpty())
						{
							int nResult = m_udpSocket->writeDatagram(data, data.length(), addr, port);
						}
					}
					else if (step == 1)//timeout
					{
						qDebug() << "timeout";
						step = 2;
					}
					else if (step == 2)//err
					{
						step = 0;
						qDebug() << "err";

						data = QByteArray::fromHex("55 A1 04 02 00 00 00 00 00 00 00 01 07 ff ee");
						if (!data.isEmpty())
						{
							int nResult = m_udpSocket->writeDatagram(data, data.length(), addr, port);
						}
					}
				}
			}
		});

	serverEntity = new TCPServerEntity("192.168.126.56", 5070);
}

TCPServer::~TCPServer()
{
    delete ui;
}

void TCPServer::conn()
{
	QTcpSocket* tmpSocket = m_tcpServer->nextPendingConnection();
	qDebug() << tmpSocket->peerAddress().toString();
	qDebug() << tmpSocket->peerPort();

	//connect(tmpSocket, &QAbstractSocket::disconnected, tmpSocket, &QObject::deleteLater);
	connect(tmpSocket, &QAbstractSocket::readyRead, [=]()
		{
			//qDebug() << "TcpBusiness echo thread" << QThread::currentThread() << QThread::currentThreadId() << this->thread();

			if (!tmpSocket)
				return;

			QByteArray data = tmpSocket->readAll();
			qDebug() << "recv data" << data.toHex(' ');

			{
				static int step = 0;
				if (step == 0)//OK
				{
					step = 0;
					qDebug() << data.toHex(' ');


					data = QByteArray::fromHex("55 A0 03 02 00 00 00 00 00 00 00 00 05 ff ee");
					if (!data.isEmpty())
					{
						tmpSocket->write(data);
					}
				}
				else if (step == 1)//timeout
				{
					qDebug() << "timeout";
					step = 2;
				}
				else if (step == 2)//err
				{
					step = 0;
					qDebug() << "err";

					data = QByteArray::fromHex("55 A0 03 02 00 00 00 00 00 00 00 01 06 ff ee");
					if (!data.isEmpty())
					{
						tmpSocket->write(data);
					}
				}
			}
		});
}

TCPServerEntity::TCPServerEntity(QString ip, int port)
{
	m_tcpServer = new QTcpServer(this);
	m_tcpServer->listen(QHostAddress(ip), port);
	connect(m_tcpServer, &QTcpServer::newConnection, this, &TCPServerEntity::conn);
}

TCPServerEntity::~TCPServerEntity()
{
}

void TCPServerEntity::conn()
{
	QTcpSocket* tmpSocket = m_tcpServer->nextPendingConnection();
	qDebug() << tmpSocket->peerAddress().toString();
	qDebug() << tmpSocket->peerPort();

	connect(tmpSocket, &QAbstractSocket::disconnected, tmpSocket, &QObject::deleteLater);
	connect(tmpSocket, &QAbstractSocket::readyRead, [=]()
		{
			if (!tmpSocket)
				return;

			QByteArray data = tmpSocket->readAll();
			qDebug() << "recv data" << data.toHex(' ');
		});
}

